package cn.yanhu.kuwanapp.my.activity

import android.content.Context
import android.content.Intent
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.adapter.OnItemClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespMsgBean
import cn.yanhu.kuwanapp.common.activity.CommonWebviewActivity
import cn.yanhu.kuwanapp.databinding.ActivityMessageBinding
import cn.yanhu.kuwanapp.http.status.ErrorCode
import cn.yanhu.kuwanapp.my.adapter.MessageAdapter
import cn.yanhu.kuwanapp.user.UserViewModel
import cn.yanhu.kuwanapp.utils.ext.parseState
import cn.yanhu.kuwanapp.utils.ext.parseState2
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


/**
 * 消息列表
 */
class MessageActivity : BaseActivity<ActivityMessageBinding, UserViewModel>() {

    private val mMessageDatas by lazy {
        mutableListOf<RespMsgBean>()
    }

    private val mMessageAdapter by lazy {
        MessageAdapter( mMessageDatas)
    }


    override fun setContentView(): Int = R.layout.activity_message

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //msg list
        mViewModel.msgListInfo.observe(this, Observer { it ->
            parseState(it,{
                if (endId == "0"){
                    mMessageDatas.clear()
                }else{
                    if (it.isNullOrEmpty()) mBinding.refreshLayout.finishLoadMoreWithNoMoreData() else mBinding.refreshLayout.finishLoadMore()
                }
                mMessageDatas.addAll(it)
                if (mMessageDatas.isEmpty()){
                    showEmpty()
                }
                mMessageAdapter.notifyDataSetChanged()
            })
        })
        //read msg
        mViewModel.readMsgInfo.observe(this, Observer { it ->
            parseState2(it,{
                if (it?.code == ErrorCode.SUCCESS){
                    mMessageDatas[currentPosition].haveRead = "1"
                    mMessageAdapter.notifyItemChanged(currentPosition)
                }
            })
        })
    }

    override fun showEmpty() {
        mBinding.refreshLayout.isVisible = false
        mBinding.llEmpty.isVisible = true
    }

    override fun showLoading(showContent: Boolean) {
        super.showLoading(showContent)
        mBinding.refreshLayout.isVisible = true
        mBinding.llEmpty.isVisible = false
    }

    override fun initData() {
        setStatusBarStyle(false)
        initWidgets()
        requestData()
    }

    private val mPageSize = 20
    private var endId = "0"

    override fun requestData() {
        mViewModel.msgList(endId,mPageSize)
    }

    private var currentPosition = 0

    private fun initWidgets() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (mMessageDatas.isEmpty()){
                    refreshLayout.finishLoadMoreWithNoMoreData()
                }else{
                    endId = mMessageDatas[mMessageDatas.size - 1].id
                    requestData()
                }
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                refreshLayout.finishRefresh()
                requestData()
            }
        })

        mBinding.rvList.adapter = mMessageAdapter
        mMessageAdapter.mItemClickListener = OnItemClickListener {
            val respMsgBean = mMessageDatas[it]
            if (respMsgBean.haveRead != "1"){//如果消息未读
                currentPosition = it
                mViewModel.readMsg(respMsgBean.id)
            }
            if (respMsgBean.pageUrl.isNotBlank()){
                CommonWebviewActivity.start(mContext,respMsgBean.pageUrl)
            }
        }

    }

    companion object{
        fun start(context: Context){
            val intent = Intent(context, MessageActivity::class.java)
            context.startActivity(intent)
        }
    }

}